package com.lul15834.orderfood.activity.manager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.bean.UserBean;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.until.FileImgUntil;
import com.lul15834.orderfood.until.Tools;

//实现修改信息
public class UpdateMes extends AppCompatActivity {
    private EditText mBusinessName;
    private EditText mBusinessDes;
    private EditText mBusinessType;
    private ImageView mBusinessImg;
    private Button mBusinessBtn;
    private Uri uri;
    private ActivityResultLauncher<String> getContentLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_update_mes);
        Toolbar toolbar=findViewById(R.id.update_business_back);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UpdateMes.this,Manager.class);
                intent.putExtra("sta","1");
                startActivity(intent);
            }
        });
        //加载商家的个人信息
        UserBean user=AdminDao.getBusinessUser(Tools.getOnAccount(this));
        mBusinessName=findViewById(R.id.update_business_name);
        mBusinessName.setText(user.getsName());
        mBusinessDes=findViewById(R.id.update_business_des);
        mBusinessDes.setText(user.getsDescribe());
        mBusinessType=findViewById(R.id.update_business_type);
        mBusinessType.setText(user.getsType());
        mBusinessImg=findViewById(R.id.update_business_img);
        Bitmap bitmap=BitmapFactory.decodeFile(user.getsImg());
        mBusinessImg.setImageBitmap(bitmap);//加载头像
        mBusinessImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getContentLauncher.launch("image/*");

            }
        });
        getContentLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if (result != null) {
                    mBusinessImg.setImageURI(result);
                    uri=result;
                } else {
                    Toast.makeText(UpdateMes.this, "未选择图片", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mBusinessBtn=findViewById(R.id.update_business_btn);

        Drawable drawable=mBusinessImg.getDrawable();
        Bitmap  defaultDrawable =((BitmapDrawable) drawable).getBitmap();//获取图片的二进制文件

        //点击注册修改有对应的提示
        mBusinessBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=mBusinessName.getText().toString().trim();
                String des=mBusinessDes.getText().toString().trim();
                String type=mBusinessType.getText().toString().trim();
                if (name.isEmpty()){
                    mBusinessName.setError("店铺名不能为空");
                    mBusinessName.requestFocus();
                }else if (des.isEmpty()){
                    mBusinessDes.setError("店铺描述不能为空");
                    mBusinessDes.requestFocus();
                }else if (type.isEmpty()){
                    mBusinessType.setError("店铺类型不能为空");
                    mBusinessType.requestFocus();
                }else {
                    Drawable drawable=mBusinessImg.getDrawable();
                    Bitmap bitmap =((BitmapDrawable) drawable).getBitmap();//获取图片的二进制文件
                    String path= FileImgUntil.getImgName();//图片存储路径名字
                    //检查Bitmap是否与默认鱼片相同
                    if (bitmap.sameAs(defaultDrawable)){//判断是不是默认图片
                        path=user.getsImg();

                    }else {
                        FileImgUntil.saveImageBitmapToFileImg(uri,UpdateMes.this,path);
                    }
                    int a=AdminDao.UpdateBusinessUser(user.getsId(),name,des,type,path);
                    if (a==1){
                        Toast.makeText(UpdateMes.this, "更改成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(UpdateMes.this, "更改失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

}